package com.x.provider.customer.service.customer;

import com.x.core.domain.SuggestionTypeEnum;
import com.x.provider.api.customer.model.event.CustomerInfoGreenEvent;
import com.x.provider.api.customer.model.event.FollowEvent;
import com.x.provider.api.mc.enums.AttributeNameSystem;
import com.x.provider.api.mc.model.dto.AttributeSetReqDTO;
import com.x.provider.api.mc.service.McHelper;
import com.x.provider.api.mc.service.MessageRpcService;
import com.x.provider.api.oss.service.OssRpcService;
import com.x.provider.customer.configure.CustomerApplicationConfig;
import com.x.provider.customer.enums.AttributeKeyGroupEnum;
import com.x.provider.customer.enums.SystemResumeAttributeName;
import org.springframework.stereotype.Service;

@Service
public class CustomerMcServiceImpl implements CustomerMcService {

    private final MessageRpcService messageRpcService;
    private final CustomerService customerService;
    private final OssRpcService ossRpcService;
    private final CustomerApplicationConfig applicationConfig;

    public CustomerMcServiceImpl(MessageRpcService messageRpcService,
                                 CustomerService customerService,
                                 OssRpcService ossRpcService,
                                 CustomerApplicationConfig applicationConfig){
        this.messageRpcService = messageRpcService;
        this.customerService = customerService;
        this.ossRpcService = ossRpcService;
        this.applicationConfig = applicationConfig;
    }

    @Override
    public void onCustomerInfoGreen(CustomerInfoGreenEvent event) {
        messageRpcService.sendMessage(McHelper.buildUserInfoGreenNotify(event.getCustomerId(), event.isPass()));
    }

    @Override
    public void onFollowEvent(FollowEvent followEvent) {
        if (FollowEvent.EventTypeEnum.FOLLOW.getValue().equals(followEvent.getEventType()) && !followEvent.isFirstFollow()){
            return;
        }
//        String nickName = this.customerService.listAndFillDefaultCustomerAttribute(CustomerResumeAttributeQuery.builder().suggestionType(SuggestionTypeEnum.PASS)
//                .customerIdList(Arrays.asList(followEvent.getFromCustomerId())).build()).get(followEvent.getFromCustomerId()).stream()
//                .filter(item -> SystemResumeAttributeName.NICK_NAME.name().equals(item.getKey())).findFirst().get().getValue();
//        messageRpcService.sendMessage(McHelper.buildFansNotify(followEvent.getToCustomerId(), nickName));
    }

    @Override
    public void onNickNameSave(Long entityId, String key, String value){
        if (!SystemResumeAttributeName.NICK_NAME.name().equals(key)){
            return;
        }
        onResumeAttributeSave(AttributeKeyGroupEnum.RESUME.name(), entityId, key, value);
    }

    @Override
    public void onCustomerCreate(Long customerId) {
        onNickNameSave(customerId, SystemResumeAttributeName.NICK_NAME.name(), applicationConfig.toString());
        onResumeAttributeSave(AttributeKeyGroupEnum.RESUME.name(), customerId, SystemResumeAttributeName.AVATAR_ID.name(),
                ossRpcService.getObjectBrowseUrl(applicationConfig.getDefaultAvatarId()).getData());
    }

    @Override
    public void onResumeAttributeSave(String group, Long entityId, String key, String value){
        if (!AttributeKeyGroupEnum.RESUME.name().equals(group)){
            return;
        }
        AttributeSetReqDTO attributeSetReqDTO = null;
        if(SystemResumeAttributeName.NICK_NAME.name().equals(key)){
            attributeSetReqDTO = AttributeSetReqDTO.builder().entityId(entityId).key(AttributeNameSystem.NICK_NAME.name()).value(value).build();
        }
        if (SystemResumeAttributeName.AVATAR_ID.name().equals(key)){
            String url = ossRpcService.getObjectBrowseUrl(value).getData();
            attributeSetReqDTO = AttributeSetReqDTO.builder().entityId(entityId).key(AttributeNameSystem.AVATAR_URL.name()).value(url).build();
        }
        attributeSetReqDTO.setSuggestionType(SuggestionTypeEnum.PASS.getValue());
        messageRpcService.setCustomerResume(attributeSetReqDTO);
    }
}
